﻿using System;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System.Web;
using System.Web.UI;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.ObjectBuilder2;

namespace SGRapidForm.Common
{
    [ConfigurationElementType(typeof(AlertHandlerData))]
    public class AlertHandler : IExceptionHandler
    {

        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            Page handler = HttpContext.Current.Handler as Page;
            if (handler != null)
            {
                string script = string.Format("<Script language=\"javascript\">alert(\"{0}\");</Script>",exception.Message);
                handler.ClientScript.RegisterStartupScript(this.GetType(), "ExceptionHandling.AlertHandler", script);
            }
            return exception;
        }
    }

    [Assembler(typeof(AlertHandlerAssembler))]
    public class AlertHandlerData : ExceptionHandlerData
    { }

    public class AlertHandlerAssembler : IAssembler<IExceptionHandler, ExceptionHandlerData>
    {

        #region IAssembler<IExceptionHandler,ExceptionHandlerData> Members

        public IExceptionHandler Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            return new AlertHandler();
        }

        #endregion
    }
}
